package wiki.parser
{
	/**
	 * An instance of this class represents a wiki pattern, which consists of a regular 
	 * expression (RegExp) and a replacement string.
	 * 
	 * The main function of a WikiPattern is to parse a string and to replace all occurances of
	 * a regular expression (the pattern) with a replacement string. 
	 */ 
	public class WikiPattern {
		
		/**
		 * Constructs a new WikiPattern instance.
		 * @param pattern - a regular expression representing the pattern
		 * @param replacement - the string to use when replacing occurances of the pattern expression
		 */ 
		public function WikiPattern(pattern: RegExp=null, replacement:String="") {
			_pattern = pattern;
			_replacement = replacement;
		}
		
		/**
		 * Parses the input String and returns a new String in which all occurances of the pattern
		 * are replaced by the replacement String.
		 */
		public function parse(input:String): String {
			if (_pattern!=null) {
				return input.replace(_pattern, _replacement);
			}
			return input;
		}
		
		/**
		 * Sets the pattern for this WikiPattern
		 */
		public function set pattern(value:RegExp): void {
			_pattern = value;
		}
		
		/**
		 * Sets the replacement string for this WikiPattern
		 */
		public function set replacement(value:String): void {
			_replacement = value!=null ? value : "";
		}
		
		private var _pattern:RegExp;
		private var _replacement:String;

	}
}